package com.aimosheng.common.pay.paypal;

import android.text.TextUtils;

import com.braintreepayments.api.BraintreeFragment;
import com.braintreepayments.api.exceptions.BraintreeError;
import com.braintreepayments.api.exceptions.ErrorWithResponse;
import com.braintreepayments.api.exceptions.InvalidArgumentException;
import com.braintreepayments.api.interfaces.BraintreeCancelListener;
import com.braintreepayments.api.interfaces.BraintreeErrorListener;
import com.braintreepayments.api.interfaces.PaymentMethodNonceCreatedListener;
import com.braintreepayments.api.models.PaymentMethodNonce;
import com.aimosheng.common.activity.AbsActivity;

/**
 * Des:
 * <p>
 * Create by W_X on 2021/03/26.10:55 AM.
 */
public final class BraintreeUtils {
    public static BraintreeFragment init(AbsActivity activity, final String mToken) {
        try {
            BraintreeFragment mBraintreeFragment = BraintreeFragment.newInstance(activity, mToken);
            // 支付完成监听
            mBraintreeFragment.addListener(new PaymentMethodNonceCreatedListener() {
                @Override
                public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
                    String nonce = paymentMethodNonce.getNonce();
                    if (mListener != null) {
                        if (!TextUtils.isEmpty(nonce)) {
                            mListener.paySuccess(nonce);
                        } else {
                            mListener.payFail();
                        }
                    }
                }
            });
            // 支付取消监听
            mBraintreeFragment.addListener(new BraintreeCancelListener() {
                @Override
                public void onCancel(int requestCode) {
                    if (mListener != null) {
                        mListener.payCancel();
                    }
                }
            });
            // 支付错误监听
            mBraintreeFragment.addListener(new BraintreeErrorListener() {
                @Override
                public void onError(Exception error) {
                    if (error instanceof ErrorWithResponse) {
                        ErrorWithResponse errorWithResponse = (ErrorWithResponse) error;
                        BraintreeError cardErrors = errorWithResponse.errorFor("creditCard");
                        if (cardErrors != null) {
                            BraintreeError expirationMonthError = cardErrors.errorFor("expirationMonth");
                            if (expirationMonthError != null) {
                                if (mListener != null) {
                                    mListener.payFail();
                                }
                            }
                        }
                    }
                }
            });
            return mBraintreeFragment;
        } catch (InvalidArgumentException e) {
            if (mListener != null) {
                mListener.payFail();
            }
            return null;
        }
    }

    private static IPayResultListener mListener;

    public interface IPayResultListener {

        void paySuccess(String nonce);

        void payCancel();

        void payFail();
    }

    public static void setPayResultListener(IPayResultListener listener) {
        mListener = listener;
    }
}
